package com.yuanfeng.biz.front.buy.web;

import com.yuanfeng.entity.Books;
import com.yuanfeng.util.BaseServlet;
import com.yuanfeng.util.WebUtil;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 购物车模块的servlet
 *
 * @author yuanfeng
 *
 */
public class BuyServlet extends BaseServlet {

	// 把一本书加到购物车中
	public void addToCat(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		HttpSession session = request.getSession(true);

		// 1)接收用户购买的图书的信息
		Books book = WebUtil.copyRequestToBean(request, Books.class);
		// 2)创建一个Map集合来保存购物车的所有图书信息
		// 先从session中读取购物车的数据
		Map<String, Books> car = (Map<String, Books>) session.getAttribute("car");
		if (car == null) {
			// 第一次访问
			car = new HashMap<String, Books>();
		}
		/**
		 * 数量问题： 1）如果是当前购物车中没有这本书，则为1 2）如果当前购物车中有了这本书，则为 （当前数量+1）
		 */
		if (car.containsKey(book.getId())) {
			// 获取购物现有的数量+1
			book.setAmt(car.get(book.getId()).getAmt() + 1);
		} else {
			book.setAmt(1);
		}

		// 3)把图书放入购物车对象中
		car.put(book.getId(), book);

		// 4)把购物车对象放入session中
		session.setAttribute("car", car);

		// 5)跳转到购物车jsp页面
		response.sendRedirect(request.getContextPath() + "/jsp/front/buy/car.jsp");
	}

	// 修改购物车的图书数量(加或减)
	public void modifyCar(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String bookId = request.getParameter("id");
		String num = request.getParameter("num");// 获取是+还是-
		Integer add = Integer.parseInt(num);
		bookId = bookId.trim();
		Map<String, Books> car = (Map<String, Books>) request.getSession().getAttribute("car");
		int result = 0;
		if (car.containsKey(bookId)) {
			Books book = car.get(bookId);
			book.setAmt(book.getAmt() + add);
			if (book.getAmt() == 0) {
				car.remove(bookId);
				result = 0;
			}
			result = book.getAmt();
		}
		response.sendRedirect(request.getContextPath() + "/jsp/front/buy/car.jsp");
	}

	// 清空session购物车中的数据
	public void clearCar(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 删除session数据
		request.getSession().removeAttribute("car");
		// 重新跳转到购物车首页
		request.getRequestDispatcher("/jsp/front/buy/car.jsp").forward(request, response);

	}

}